import 'package:flutter/material.dart';
import 'package:we_chat/pages/chat/chat_page.dart';
import 'package:we_chat/pages/discover/discover_page.dart';
import 'package:we_chat/pages/friends/friends_page.dart';
import 'package:we_chat/pages/mine_page.dart';


class Root_Page extends StatefulWidget {
  const Root_Page({Key? key}) : super(key: key);

  @override
  State<Root_Page> createState() => _Root_PageState();
}

class _Root_PageState extends State<Root_Page> {
  int _currentIndex = 0;
  List<Widget> _pages = [ChatPage(),FriendsPage(),DiscoverPage(),MinePage()];
  final PageController _controller = PageController(initialPage: 0);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //没有在渲染树中就释放了，下次会重写创建
      // body: _pages[_currentIndex],
      body: PageView(
        //滚动tabbar跟着切换
        onPageChanged: (index){
          setState((){
            _currentIndex = index;
          });
        },
        //不滚动
        physics: NeverScrollableScrollPhysics(),
        controller: _controller,
        children: _pages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: (index){
          setState((){
            _currentIndex = index;
          });
          _controller.jumpToPage(index);
        },//传递选中index
        selectedFontSize: 12,//选中字体，跟未选中默认一样
        currentIndex: _currentIndex,//选择的
        fixedColor: Colors.green,//选择颜色，默认选择为白色
        type: BottomNavigationBarType.fixed,//类型，默认不选为白色
        items: [
          BottomNavigationBarItem(
            icon: Image.asset('images/tabbar_chat.png',height: 20,width: 20,),
            activeIcon: Image.asset('images/tabbar_chat_hl.png',height: 20,width: 20,),//图片太大，要限制图片大小
            label: '微信',
          ),
          BottomNavigationBarItem(
            icon: Image.asset('images/tabbar_friends.png',height: 20,width: 20,),
            activeIcon: Image.asset('images/tabbar_chat_hl.png',height: 20,width: 20,),
            label: '通讯录',
          ),
          BottomNavigationBarItem(
            icon: Image.asset('images/tabbar_discover.png',height: 20,width: 20,),
            activeIcon: Image.asset('images/tabbar_discover_hl.png',height: 20,width: 20,),
            label: '发现',
          ),
          BottomNavigationBarItem(
            icon: Image.asset('images/tabbar_mine.png',height: 20,width: 20,),
            activeIcon: Image.asset('images/tabbar_mine_hl.png',height: 20,width: 20,),
            label: '我',
          ),
        ],
      ),
    );
  }
}
